<?php

namespace app\admin\model\scanwork;

use think\Model;

/**
 * scanwork配置模型
 */
class Config extends Model
{
    // 表名
    protected $name = 'scanwork_config';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    /**
     * 获取配置值
     */
    public static function getValue($key, $default = '')
    {
        $config = self::where('key', $key)->where('status', 1)->find();
        if ($config) {
            return $config->value;
        }
        return $default;
    }
    
    /**
     * 设置配置值
     */
    public static function setValue($key, $value)
    {
        $config = self::where('key', $key)->find();
        if ($config) {
            $config->value = $value;
            $config->updatetime = time();
            return $config->save();
        }
        return false;
    }
    
    /**
     * 获取所有配置
     */
    public static function getAllConfigs()
    {
        return self::where('status', 1)
            ->order('sort', 'asc')
            ->select();
    }
    
    /**
     * 批量设置配置
     */
    public static function setConfigs($data)
    {
        $result = true;
        foreach ($data as $key => $value) {
            if (!self::setValue($key, $value)) {
                $result = false;
            }
        }
        return $result;
    }
    
    /**
     * 获取网站域名
     */
    public static function getSiteDomain()
    {
        return self::getValue('site_domain', 'https://order.023ent.net');
    }
    
    /**
     * 获取小程序AppID
     */
    public static function getMiniappAppid()
    {
        return self::getValue('miniapp_appid', '');
    }
    
    /**
     * 获取小程序AppSecret
     */
    public static function getMiniappSecret()
    {
        return self::getValue('miniapp_secret', '');
    }
    
    /**
     * 获取小程序名称
     */
    public static function getMiniappName()
    {
        return self::getValue('miniapp_name', '生产管理小程序');
    }
    
    /**
     * 获取七牛云Access Key
     */
    public static function getQiniuAccessKey()
    {
        return self::getValue('qiniu_access_key', '');
    }
    
    /**
     * 获取七牛云Secret Key
     */
    public static function getQiniuSecretKey()
    {
        return self::getValue('qiniu_secret_key', '');
    }
    
    /**
     * 获取七牛云Bucket
     */
    public static function getQiniuBucket()
    {
        return self::getValue('qiniu_bucket', '');
    }
    
    /**
     * 获取七牛云域名
     */
    public static function getQiniuDomain()
    {
        return self::getValue('qiniu_domain', '');
    }
    
    /**
     * 获取七牛云是否启用
     */
    public static function getQiniuEnabled()
    {
        return self::getValue('qiniu_enabled', '0') == '1';
    }
    
    /**
     * 获取溯源码URL
     */
    public static function getTraceUrl($traceCode)
    {
        $domain = self::getSiteDomain();
        $prefix = self::getValue('trace_url_prefix', '/trace.html?id=');
        return $domain . $prefix . $traceCode;
    }
    
    /**
     * 获取报工二维码URL
     */
    public static function getReportQrUrl($allocationId)
    {
        $domain = self::getSiteDomain();
        return $domain . '/index/worker/report/id/' . $allocationId . '.html';
    }
    
    /**
     * 获取完整图片URL
     */
    public static function getFullImageUrl($imagePath)
    {
        if (empty($imagePath)) {
            return '';
        }
        
        // 如果已经是完整URL，直接返回
        if (strpos($imagePath, 'http') === 0) {
            return $imagePath;
        }
        
        // 添加域名前缀
        $domain = self::getSiteDomain();
        return $domain . $imagePath;
    }
}
